﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChurchData.Business
{
    public class HouseholdBO : ChurchDataBusinessBase
    {
        public Household AddRecord()
        {
            ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString());
            Household rec = new Household();
            oDB.Households.InsertOnSubmit(rec);
            rec.LastUpdTimestamp = DateTime.Now;
            rec.LastUpdUser = UserInformation.UserName;
            rec.ActiveInd = true;
            oDB.SubmitChanges();
            return rec;
        }

        public void UpdateFamilyPicture(int householdPK, System.Data.Linq.Binary img)
        {
            ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString());
            Household rec = (from h in oDB.Households
                             where h.HouseholdPK == householdPK
                             select h).First();
            rec.FamilyPicture = img;
            rec.LastUpdTimestamp = DateTime.Now;
            rec.LastUpdUser = UserInformation.UserName;
            oDB.SubmitChanges();
        }

        /// <summary>
        /// Remove the head of household indicator from the current head and mark the parmed peoplePK the new head of household
        /// </summary>
        /// <param name="householdPK"></param>
        /// <param name="peoplePK"></param>
        public void UpdateHeadOfHousehold(int householdPK, int peoplePK)
        {
            ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString());

            IEnumerable<People> householdMembers = from p in oDB.Peoples
                                                   where p.HouseholdFK == householdPK
                                                   select p;

            foreach (People p in householdMembers)
            {
                if (p.PeoplePK == peoplePK)
                    p.HeadOfHouseInd = true;
                else
                    p.HeadOfHouseInd = false;
            }

            oDB.SubmitChanges();
        }
    }
}
